package vn.com.pps.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

    private static final int BIN_DIGITS = 12;

    public static final short ONE_MS = 1 << BIN_DIGITS;

    public static String getDatePattern() {
        return "dd/MM/yyyy";
    }
    
    public static String getTimePattern() {
        return "yyyyMMddHHmmss";
    }
    
    public static Date getDateTime(Date date,String format)throws ParseException{
        SimpleDateFormat df = new SimpleDateFormat(format);
        String strDate = df.format(date);
        Calendar cal = new GregorianCalendar();
        cal.setTime(stringToDate(strDate,format));
        return cal.getTime();
    }
    
    public static int getYear(){
    	return Calendar.getInstance().get(Calendar.YEAR);
    }
    
    public static int getYear(Date date){
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
    	return cal.get(Calendar.YEAR);
    }
    
    public static int getMonth(){
    	return Calendar.getInstance().get(Calendar.MONTH)+1;
    }
    
    public static int getMonth(Date date){
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
    	return cal.get(Calendar.MONTH)+1;
    }
    
    public static int getDayOfMonth(){
    	return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }
    
    public static int getDayOfMonth(Date date){
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
    	return cal.get(Calendar.DAY_OF_MONTH);
    }
    
    public static final Date stringToDate(String strDate,String format)throws ParseException {
        SimpleDateFormat df = null;
        Date date = null;
        df = new SimpleDateFormat(format);

        try {
            date = df.parse(strDate);
        } catch (ParseException pe) {
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());
        }
        return (date);
    }

    public static Date stringToDate(String strDate)throws ParseException {
        Date aDate = null;
        try {
            aDate = stringToDate(strDate,getDatePattern());
        } catch (ParseException pe) {
            pe.printStackTrace();
            throw new ParseException(pe.getMessage(),pe.getErrorOffset());
        }
        return aDate;
    }
    
    public static final String dateToString(Date aDate,String format) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate == null) {
            //
        } else {
            df = new SimpleDateFormat(format);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }
    
    public static final String dateToString(Date aDate) {
        return dateToString(aDate,getDatePattern());
    }
    
    public static final String timeToString(Date aDate) {
        return dateToString(aDate,getTimePattern());
    }
    
    public static Date getDate(Date aDate)throws ParseException{
    	return getDateTime(aDate,getDatePattern());
    }
    
    public static Date getTime(Date aDate)throws ParseException{
    	return getDateTime(aDate,getTimePattern());
    }
    
    public static long getDaysBetween(Date dtFrom, Date dtEnd) {
        long begin = dtFrom.getTime();
        long end = dtEnd.getTime();
        long inter = end - begin;
        if (inter < 0) {
            inter = inter * (-1);
        }
        long dateMillSec = 24 * 60 * 60 * 1000;
       
        long dateCnt =  inter / dateMillSec;
       
        long remainder = inter % dateMillSec;
       
        if (remainder != 0) {
            dateCnt++;
        }
        return dateCnt;
    }

}
